package com.boot.security.server.controller.system;

import com.boot.security.server.dao.mffl.MfflInfomationContentDao;
import com.boot.security.server.dao.mffl.MfflInformationDao;
import com.boot.security.server.dao.mffl.MfflInformationReplyDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflInfomationContent;
import com.boot.security.server.model.mffl.MfflInformation;
import com.boot.security.server.model.mffl.Vo.MfflInformationVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.MfflUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "后台资讯管理")
@RestController
@RequestMapping("/mfflInformations")
public class MfflInformationController {

    @Autowired
    private MfflInformationDao mfflInformationDao;

    @Autowired
    private MfflInfomationContentDao mfflInfomationContentDao;

    @Autowired
    private MfflInformationReplyDao mfflInformationReplyDao;

    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:informations:add')")
    @ApiOperation(value = "保存")
    public MfflInformation save(@RequestBody MfflInformation mfflInformation) {
        SysUser sysUser = UserUtil.getLoginUser();
        mfflInformation.setUserId(sysUser.getId().intValue());
        String partialContent = MfflUtil.delHTMLTag(mfflInformation.getContent());
        if (partialContent.length() > 250){
            partialContent = partialContent.substring(0,249);
        }
        mfflInformation.setPartialContent(partialContent);
        mfflInformation.setImageUrl(mfflInformation.getImageUrl().replace(",",";"));
        int status = mfflInformationDao.save(mfflInformation);
        if (status == 1) {
            MfflInfomationContent mfflInfomationContent = new MfflInfomationContent();
            mfflInfomationContent.setInfoId(mfflInformation.getId().intValue());
            mfflInfomationContent.setContent(mfflInformation.getContent());
            mfflInfomationContentDao.save(mfflInfomationContent);
        }
        return mfflInformation;
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:informations:query')")
    @ApiOperation(value = "根据id获取")
    public MfflInformation get(@PathVariable Long id) {
        return mfflInformationDao.getById(id);
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:informations:add')")
    @ApiOperation(value = "修改")
    public MfflInformation update(@RequestBody MfflInformation mfflInformation) {
        String partialContent = MfflUtil.delHTMLTag(mfflInformation.getContent());
        if (partialContent.length() > 250){
            partialContent = partialContent.substring(0,249);
        }
        mfflInformation.setPartialContent(partialContent);
        mfflInformation.setImageUrl(mfflInformation.getImageUrl().replace(",",";"));
        int status = mfflInformationDao.update(mfflInformation);
        if (status == 1){

            this.mfflInfomationContentDao.updateContentById(mfflInformation.getId(),mfflInformation.getContent());
        }

        return mfflInformation;
    }

    @GetMapping
    @PreAuthorize("hasAnyAuthority('sys:informations:query')")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflInformationDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflInformation> list(PageTableRequest request) {
                return mfflInformationDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:informations:del')")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {

        int status = this.mfflInformationDao.delMyInformation(id);
        if (status == 1) {
            this.mfflInformationReplyDao.delReplyByInformation(id);
        }
    }
}
